package com.tangsm.api.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tangsm.api.demo.annotation.ResponseResultBody;
import com.tangsm.api.demo.domain.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author 漡神码
 * @date 20201114
 */
@RestController
@RequestMapping("/valid")
@ResponseResultBody
public class ValidationController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * String 返回验证
     * @return String
     */
    @GetMapping("/str")
    public String stringResult(){
        logger.info("stringResult...");
        logger.info("测试Jrebel...");
        logger.info("测试Jrebel...");
        return "成功";
    }

    /**
     * 新增
     * @param userInfo
     * @return
     */
    @PostMapping("/add")
    public UserInfo add(@RequestBody @Valid UserInfo userInfo) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        String userInfoToString = mapper.writeValueAsString(userInfo);
        logger.info("add User Info...{}", userInfoToString);
        return userInfo;
    }
}
